import { GCtrl } from "../../../../lib/util/GCtrl";
const { ccclass, property } = cc._decorator;
/** 复杂聊天 聊天item create by tm */
@ccclass
export default class DWC_DCContentItem extends cc.Component {
    //-----------组件区----------
    //聊条内容
    @property(cc.Node)
    node_chat: cc.Node = null;
    @property(cc.Node)
    node_head: cc.Node = null;
    @property(cc.Sprite)
    img_head: cc.Sprite = null;
    @property(cc.Sprite)
    img_chatBg: cc.Sprite = null;
    @property(cc.Label)
    lbl_nick: cc.Label = null;
    @property(cc.Label)
    lbl_chat: cc.Label = null;
    @property(cc.Label)
    lbl_chatL: cc.Label = null;
    @property(cc.Sprite)
    img_yuyin: cc.Sprite = null;
    @property(cc.Sprite)
    img_emoticon: cc.Sprite = null;
    //其他信息
    @property(cc.Label)
    lbl_info: cc.Label = null;
    //-----------变量区----------
    /** 内容 */
    dccontent: DWC_DCContentData = null;
    /** 内容相关 */
    conentLimit: {
        /** 聊天内容最小宽度 122*/
        minL: number;
        /** 聊天内容最大宽度 300*/
        maxL: number;
        /** 聊天背景和内容宽度差 15 */
        wDif: number;
        /** 聊天背景和内容高度度差 15 */
        hDif: number;
    } = null;
    /** item idx(当前item在父级的下标) */
    itemIdx: number = null;
    onLoad() {
        this.conentLimit = {
            minL: 122,
            maxL: 300,
            wDif: 15,
            hDif: 15
        }
    }
    /** 初始化(外界调用) */
    init(_args): void {
        this.dccontent = {
            style: _args.style,
            type: _args.type,
            content: _args.content,
            head: _args.head,
            nick: _args.nick,
            rid: _args.rid,
            itemY: _args.itemY,
            height: _args.height
        }
        this.contentStyle(_args.style);
        if (this.dccontent.style != 3) {
            this.lbl_nick.string = cc.vv.GLUtils.getShortStr(_args.nick || ``, 6);
            this.contentType(this.dccontent.type);
            //设置头像
            let _head: string = this.dccontent.head;
            if (!_head) {
                _head = `1`;
            }
            GCtrl.setHeadSpr(_head, this.img_head);
            this.img_head.node.height = 80;
            this.img_head.node.width = 80;
        } else {
            this.setInfoContent(this.dccontent.content);
        }
    }
    /** 内容样式 */
    contentStyle(_style: number): void {
        if (!this.dccontent) return;
        let _isChat: boolean = _style != 3;
        this.lbl_info.node.active = !_isChat;
        this.node_chat.active = _isChat;
        if (_isChat) {
            let _isChatLeft = _style == 1;
            this.setWidget(this.node_head, 30, _isChatLeft);
            this.setWidget(this.img_chatBg.node, 128, _isChatLeft);
            this.setWidget(this.lbl_nick.node, 142, _isChatLeft);
            this.setWidget(this.lbl_chat.node, _isChatLeft ? 20 : 4, true);
            this.img_chatBg.node.scaleX = _isChatLeft ? 1 : -1;
            this.lbl_chat.node.scaleX = _isChatLeft ? 1 : -1;
        }
    }
    /** 初始化ui */
    initui(): void {
        //信息
        this.lbl_info.string = ``;
        this.lbl_info.node.active = false;
        this.node_chat.active = true;
        //聊天txt
        this.lbl_chat.string = ``;
        this.lbl_chatL.string = ``;
        this.lbl_chat.node.active = true;
        this.lbl_chatL.node.active = false;
        //表情
        this.img_emoticon.node.active = false;
        //语音
        this.img_yuyin.node.active = false;
        //背景
        this.img_chatBg.node.width = 152;
        this.img_chatBg.node.height = 50;
        //item
        this.node.height = 110;
        //数据
        this.dccontent = null;
    }
    /** 设置内容类型 
     * @param _type 聊天类型 (1:文本;2:表情;3:语音;)
    */
    contentType(_type: number): void {
        if (!_type) return;
        [this.lbl_chat.node, this.img_emoticon.node, this.img_yuyin.node].forEach((_v, _idx) => {
            _v.active = _idx == _type - 1;
        });
    }
    /** 设置节点widget 
     * @param _isLeft 是不是靠左(不是靠左就是靠右)
    */
    setWidget(_node: cc.Node, _num: number, _isLeft: boolean = false): cc.Widget {
        let _widget = _node.getComponent(cc.Widget);
        if (_widget) {
            _widget.isAlignLeft = _isLeft;
            _widget.isAlignRight = !_isLeft;
            _isLeft ? _widget.left = _num : _widget.right = _num;
        }
        return _widget;
    }
    /** 设置文本聊天内容 */
    setChatLblContent(_content: string): void {
        this.setLblContent(_content);
        //设置背景图宽
        let _lblw = this.lbl_chatL.node.width;
        if (_lblw > this.conentLimit.maxL) {
            this.img_chatBg.node.width = this.conentLimit.maxL + this.conentLimit.wDif;
        } else {
            if (_lblw > this.conentLimit.minL) {
                this.img_chatBg.node.width = Math.ceil(_lblw) + this.conentLimit.wDif + 20;
            }
            if (this.dccontent.style == 2) {
                let _widget = this.lbl_chat.node.getComponent(cc.Widget);
                _widget.left -= this.conentLimit.maxL + this.conentLimit.wDif - this.img_chatBg.node.width;
            }
        }
        //设置背景图高
        this.img_chatBg.node.height = Math.ceil(this.lbl_chat.node.height) + this.conentLimit.hDif;
        this.node.height = this.img_chatBg.node.height + 60;
    }
    /** 设置表情 */
    setEmoticonContent(_sf: cc.SpriteFrame): void {
        this.img_emoticon.spriteFrame = _sf;
    }
    /** 聊天lbl内容 */
    setLblContent(_txt: string = ``): void {
        this.lbl_chat.string = _txt;
        this.lbl_chatL.string = _txt;
        //调用一次手动渲染(lbl 宽高不是实时变化)
        this.lbl_chat[`_forceUpdateRenderData`](true);
        this.lbl_chatL[`_forceUpdateRenderData`](true);
    }
    /** 设置其他信息内容 */
    setInfoContent(_info: string): void {
        this.lbl_info.string = _info;
        //调用一次手动渲染(lbl 宽高不是实时变化)
        this.lbl_info[`_forceUpdateRenderData`](true);
        this.node.height = this.lbl_info.node.height + 60;
    }
    onDestroy() {
    }
}
/** 聊天内容数据 */
export interface DWC_DCContentData {
    /** 聊天内容样式(1:聊天左边;2:聊天右边;3:其他信息;) */
    style: number;
    /** 聊天类型 (1:文本;2:表情;3:语音;) */
    type: number;
    /** 聊天或其他信息内容 */
    content: string;
    /** 头像 */
    head: string;
    /** 昵称 */
    nick: string;
    /** rid */
    rid: number;
    /** item 在content中的y位置 */
    itemY: number;
    /** item 加入数据后的实际高度 */
    height: number;
}
